package br.com.caelum.lojavirtual.logic;

import java.util.List;

import org.vraptor.annotations.Component;
import org.vraptor.annotations.InterceptedBy;
import org.vraptor.plugin.hibernate.Validate;

import br.com.caelum.lojavirtual.dao.DaoFactory;
import br.com.caelum.lojavirtual.modelo.Cd;
import br.com.caelum.lojavirtual.modelo.Musica;

@Component("musica")
@InterceptedBy({AutorizadorInterceptor.class,DaoInterceptor.class})
public class MusicaLogic {

	private final DaoFactory daoFactory;
	private Musica musica;
	private List<Musica> musicas;
	
	public MusicaLogic(DaoFactory daoFactory) {
		this.daoFactory = daoFactory;
	}
	
	public void formulario(){
	}
	public void editar(Musica musica){
		this.musica = this.daoFactory.getMusicaDao().procura(musica.getId());
	}
	@Validate(params={"musica"})
	public void armazena(Musica musica){
		this.daoFactory.beginTransaction();
		
		if(musica.getId()==null){
			this.daoFactory.getMusicaDao().adiciona(musica);
		}else{
			this.daoFactory.getMusicaDao().atualiza(musica);
		}
		
		this.daoFactory.commit();
	}
	
	public void remove(Musica musica){
		this.daoFactory.beginTransaction();
		this.daoFactory.getMusicaDao().remove(musica);
		this.daoFactory.commit();
	}
	public void lista(){
		this.musicas = this.daoFactory.getMusicaDao().listaTudo();
	}
	public Musica getMusica() {
		return musica;
	}
	public List<Musica> getMusicas() {
		return musicas;
	}
	public List<Cd> getListaCds(){
		return this.daoFactory.getCdDao().listaTudo();
	}
}
